package com.ds.infrastructure.hyperspace.console.service.impl;

import com.ds.infrastructure.hyperspace.console.dao.dao.TbNodeMetricsDao;
import com.ds.infrastructure.hyperspace.console.entity.page.TbPageWhere;
import com.ds.infrastructure.hyperspace.console.entity.table.TbNodeMetrics;
import com.ds.infrastructure.hyperspace.console.entity.where.TbCustomWhere;
import com.ds.infrastructure.hyperspace.console.entity.where.TbNodeMetricsCustomWhere;
import com.ds.infrastructure.hyperspace.console.service.inf.INodeMetricsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: yaozhendong
 * @create: 2019-10-31 15:54
 **/

@Service
public class NodeMetricsService implements INodeMetricsService {
    @Autowired
    TbNodeMetricsDao tbNodeMetricsDao;

    @Override
    public int save(TbNodeMetrics record) {
        return tbNodeMetricsDao.insertSelective(record);
    }

    @Override
    public int update(TbNodeMetrics record) {
        return tbNodeMetricsDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public TbNodeMetrics get(Long id) {
        return tbNodeMetricsDao.selectByPrimaryKey(id);
    }

    @Override
    public int delete(Long id) {
        throw new IllegalStateException("no implement delete method");
    }

    @Override
    public List<TbNodeMetrics> getList(TbNodeMetrics where, TbCustomWhere customWhere, TbPageWhere pageWhere) {
        return tbNodeMetricsDao.selectListForPage(where, customWhere, pageWhere);
    }

    @Override
    public int getTotal(TbNodeMetrics where, TbCustomWhere customWhere) {
        return tbNodeMetricsDao.selectListForPageCount(where, customWhere);
    }

    @Override
    public TbNodeMetrics selectOne(TbNodeMetrics where, TbCustomWhere customWhere) {
        List<TbNodeMetrics> itemList = tbNodeMetricsDao.selectListForPage(where, customWhere, TbPageWhere.newPage4OnlyOne());
        return itemList.stream().findFirst().orElseGet(() -> null);
    }

    @Override
    public int insertList(List<TbNodeMetrics> recordList) {
        return tbNodeMetricsDao.insertList(recordList);
    }

    @Override
    public void clean(Long executeTime4EndTime) {
        TbNodeMetricsCustomWhere customWhere = new TbNodeMetricsCustomWhere().setExecuteTime4EndTime(executeTime4EndTime);
        int deleteRow = 0;
        do {
            deleteRow = tbNodeMetricsDao.delete(customWhere);
        } while (deleteRow > 1);
    }
}
